<template>
  <div class="page">
    <div class="header">
      <img src="@/assets/images/logo.png" alt="" />
    </div>
    <div class="center">
      <div class="center-form">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          label-position="left"
        >
          <el-form-item prop="username">
            <p>账号</p>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>
          <el-form-item prop="password">
            <p>密码</p>
            <el-input
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="1"
              auto-complete="on"
            />
            <span class="show-pwd" @click="showPwd">
              <i
                :class="
                  passwordType === 'password'
                    ? 'iconfont icon-eye1'
                    : 'iconfont icon-eye'
                "
              />
            </span>
          </el-form-item>
          <el-form-item prop="identify">
            <p>验证码</p>
            <el-input
              class="indentify-code"
              ref="identify"
              v-model="loginForm.identify"
              placeholder="请输入验证码"
              name="identify"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
            <div class="code" @click="changeImgCode">
              <img :src="code" alt="" />
            </div>
          </el-form-item>
          <el-form-item class="btn">
            <el-button
              type="primary"
              @click="submitForm('loginForm')"
              @keyup.enter="submitForm"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="bottom">
      <div class="item">
        <div class="item-title">
          <h1 class="l">政策文件</h1>
          <router-link to="/noZhengce" class="r"
            >更多<i class="iconfont icon-gengduo1"></i
          ></router-link>
        </div>
        <ul>
          <li
            v-for="item in zhengceList"
            :key="item.ac_id"
            @click="PageDetails(item.a_id)"
          >
            <p class="l">{{ item.title }}</p>
            <p class="r time">{{ item.add_time | formatTime }}</p>
          </li>
        </ul>
      </div>
      <div class="item">
        <div class="item-title">
          <h1 class="l">公示公告</h1>
          <router-link to="/noGonggao" class="r"
            >更多<i class="iconfont icon-gengduo1"></i
          ></router-link>
        </div>
        <ul>
          <li
            v-for="item in gonggaoList"
            :key="item.ac_id"
            @click="PageDetails(item.a_id)"
          >
            <p class="l">{{ item.title }}</p>
            <p class="r time">{{ item.add_time | formatTime }}</p>
          </li>
        </ul>
      </div>
    </div>
    <div class="copyright">
      <div class="box">
        <div class="box-text">
          <h1>重庆高校在线开放课程平台</h1>
          <p>
            <span>主办单位：重庆市教育委员会</span>
            <span>技术支持：重庆佰特思科技有限公司、昭信教育集团</span>
          </p>
          <p>
            <span>联系我们</span>
            <span>电话：023-67919893 023-67778602</span>
            <span>邮箱：cqooc@cqooc.com</span>
          </p>
          <p>
            <a href="http://www.cqooc.com/">渝ICP备14007033号-4</a>
            <a href="http://www.cqooc.com/"
              ><img
                src="@/assets/images/gongan.png"
              />渝公网安备：50011202501338号</a
            >
          </p>
        </div>
        <div class="box-img">
          <div>
            <img src="@/assets/images/weixin.png" alt="" />
            <p>欢迎关注公众号</p>
          </div>
          <div>
            <img src="@/assets/images/qq.png" alt="" />
            <p>欢迎加入QQ群（550104486）</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import moment from "moment";
import { UserLogin, UserInfo, ArtList } from "@/api/login";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        identify: "",
      },
      code: "http://sapi.ps.cqooc.com/unit/main/getcaptche",
      zhengceList: [],
      gonggaoList: [],
      ispage: false,
      pageNo: 1,
      pageSize: 4,
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
        ],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
        identify: [
          { required: true, trigger: "blur", message: "请输入验证码" },
        ],
      },
      passwordType: "password",
      token: "",
    };
  },
  filters: {
    formatTime(time) {
      return moment(time * 1000).format("YYYY-MM-DD");
    },
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    submitForm(loginForm) {
      this.$refs[loginForm].validate((valid) => {
        if (valid) {
          this.queryUserLogin();
        } else {
          return false;
        }
      });
    },
    // 点击刷新验证码
    changeImgCode() {
      var num = Math.ceil(Math.random() * 10); //生成一个随机数（防止缓存）
      this.code = "http://sapi.ps.cqooc.com/unit/main/getcaptche?" + num;
    },
    // 跳转页面
    PageDetails(aid) {
      this.$router.push({
        path: "/noDetails",
        query: {
          a_id: aid,
        },
      });
    },
    // 用户登录
    queryUserLogin() {
      UserLogin({
        user_name: this.loginForm.username,
        user_pass: this.loginForm.password,
        vcode: this.loginForm.identify,
      }).then((result) => {
        sessionStorage.setItem("token", result.data.token);
        this.token = sessionStorage.getItem("token");
        if (result.code === 1) {
          this.queryUserInfo();
        }
        if (result.code === -1) {
          this.changeImgCode();
        }
      });
    },
    // 获取用户信息
    queryUserInfo() {
      UserInfo({
        token: this.token,
      }).then((result) => {
        if (result.data.user_type == 1) {
          this.$router.push({
            path: "/school-home",
          });
        }
        if (result.data.user_type == 2) {
          this.$router.push({
            path: "/shenbao-home",
          });
        }
        if (result.data.user_type == 3) {
          this.$router.push({
            path: "/home",
          });
        }
      });
    },
    // 获取相关政策列表
    queryZhengCeArticleList() {
      ArtList({
        ac_id: 1,
        ispage: this.ispage,
        page_no: this.pageNo,
        page_size: this.pageSize,
      }).then((result) => {
        const {
          data: { list },
        } = result;
        this.zhengceList = list;
      });
    },
    // 获取公示公告列表
    queryGongGaoArticleList() {
      ArtList({
        ac_id: 2,
        ispage: this.ispage,
        page_no: this.pageNo,
        page_size: this.pageSize,
      }).then((result) => {
        const {
          data: { list },
        } = result;
        this.gonggaoList = list;
      });
    },
    // 点击回车键登录
    keyDown(e) {
      // 回车则执行登录方法 enter键的ASCII是13
      if (e.keyCode === 13) {
        this.queryUserLogin(); // 定义的登录方法
      }
    },
  },
  mounted() {
    window.addEventListener("keydown", this.keyDown);
    this.changeImgCode();
    this.queryZhengCeArticleList();
    this.queryGongGaoArticleList();
  },
  destroyed() {
    // 销毁事件
    window.removeEventListener("keydown", this.keyDown, false);
  },
};
</script>
<style lang='scss' scoped>
.page {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  width: 1920px;
  background-size: auto 1080px;
  padding: 0;
  color: #2b3139;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
  .header {
    display: flex;
    align-items: center;
    width: 100%;
    height: 110px;
    background-color: #ffffff;
    font-size: 30px;
    letter-spacing: 2px;
    color: #2b3139;
    img {
      width: 387px;
      height: 67px;
      margin: 0 30px 0 40px;
    }
    div {
      text-align: left;
      line-height: 40px;
    }
  }
  .center {
    height: 640px;
    padding: 100px 110px;
    background: url("../../assets/images/login-bg.png") no-repeat;
    background-size: 100% 100%;
    .center-form {
      width: 570px;
      height: 440px;
      background-color: #fff;
      float: right;
      padding: 30px 40px;
      border-radius: 10px;
      text-align: left;
      box-shadow: 0px 4px 45.57px 3.43px rgba(73, 100, 245, 0.17);
      p {
        font-size: 20px;
        color: #2b3139;
        padding: 0 15px;
        line-height: 40px;
      }
      .show-pwd {
        position: absolute;
        right: 10px;
        top: 40px;
        font-size: 24px;
        cursor: pointer;
      }
      .icon-eye {
        font-size: 24px;
      }
      .icon-eye1 {
        font-size: 20px;
      }
      /deep/.el-form-item {
        margin-bottom: 30px;
      }
      /deep/.el-input__inner {
        border-radius: 0;
        border: none;
        border-bottom: solid 2px #d7deff;
        padding: 0 15px;
        font-size: 20px;
        height: 40px;
      }
      /deep/.el-form-item__error {
        padding: 0 15px;
        line-height: 2;
        font-size: 16px;
      }
      /deep/.el-button--primary {
        background-color: #5673ff;
        border: solid 1px #2d4eff;
        padding: 10px 30px;
        font-size: 24px;
      }
      .indentify-code {
        width: 70%;
      }
      .code {
        position: absolute;
        top: 30px;
        right: 10px;
        width: 115px;
        height: 35px;
        font-size: 24px;
        color: #fff;
        text-align: center;
        background-color: #b9c5ff;
        img {
          width: 100%;
        }
      }
      .btn {
        text-align: center;
      }
    }
  }
  .bottom {
    display: flex;
    height: 335px;
    padding: 50px 100px;
    .item {
      flex: 1;
      letter-spacing: 2px;
      .item-title {
        height: 40px;
        line-height: 40px;
        margin-bottom: 15px;
        h1 {
          font-size: 34px;
          font-weight: 700;
        }
        a,
        i {
          font-size: 20px;
          color: #2b3139;
        }
        i {
          margin-left: 5px;
          font-weight: 700;
          font-size: 14px;
          vertical-align: middle;
        }
      }
      &:first-child {
        padding-right: 55px;
        border-right: solid 2px #e5e5e5;
      }
      &:last-child {
        padding-left: 55px;
      }
      ul {
        li {
          font-size: 22px;
          line-height: 45px;
          text-align: left;
          overflow: hidden;
          cursor: pointer;
          p:first-child {
            overflow: hidden;
            width: 600px;
            white-space: nowrap;
            text-overflow: ellipsis;
          }
          .time {
            color: #65bccb;
          }
        }
      }
    }
  }
  .copyright {
    background-color: #444547;
    width: 100%;
    height: 230px;
    padding: 40px 0;
    .box {
      width: 1200px;
      margin: 0 auto;
      color: #8f8e8e;
      text-align: left;
      display: flex;
      justify-content: space-between;
      font-family: "黑体";
      .box-text {
        h1 {
          margin-bottom: 40px;
          font-size: 30px;
        }
        p {
          line-height: 30px;
          font-size: 14px;
          a {
            display: inline-block;
          }
          span,
          a {
            margin-right: 20px;
            img {
              width: 22px;
              height: 22px;
              margin-right: 5px;
              vertical-align: middle;
            }
          }
        }
      }
      .box-img {
        width: 30%;
        display: flex;
        div {
          margin-left: 30px;
          width: 130px;
          img {
            height: 130px;
          }
          // a{
          //   display: block;
          // }
          p {
            margin-top: 8px;
            line-height: 15px;
            font-size: 14px;
            text-align: center;
          }
        }
      }
    }
  }
}
</style>